import {
  iosProvinces,
  iosCitys,
  iosCountys
} from "@/components/share/area/areaData_v2.js";

export default function(Vue) {
  //省份
  Vue.filter("province", function(val) {
    var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
    if (reg.test(val)) {
      return val;
    }
    if (val && val != null && val != "undefined") {
      for (var i = 0; i < iosProvinces.length; i++) {
        if (iosProvinces[i].id == val) {
          return iosProvinces[i].value;
        }
      }
    }
  });
  //市份
  Vue.filter("city", function(val) {
    var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
    if (reg.test(val)) {
      return val;
    }
    if (val && val != null && val != "undefined") {
      for (var i = 0; i < iosCitys.length; i++) {
        if (iosCitys[i].id == val) {
          return iosCitys[i].value;
        }
      }
    }
  });
  //区
  Vue.filter("district", function(val) {
    var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
    if (reg.test(val)) {
      return val;
    }
    if (val && val != null && val != "undefined") {
      for (var i = 0; i < iosCountys.length; i++) {
        if (iosCountys[i].id == val) {
          return iosCountys[i].value;
        }
      }
    }
  });
  //省市区反向过滤
    //省份
    Vue.filter("TextProvince", function(val) {
      if (val && val != null && val != "undefined") {
        for (var i = 0; i < iosProvinces.length; i++) {
          if (iosProvinces[i].value == val) {
            return iosProvinces[i].id;
          }
        }
      }
    });
    //市份
    Vue.filter("TextCity", function(val) {
      if (val && val != null && val != "undefined") {
        for (var i = 0; i < iosCitys.length; i++) {
          if (iosCitys[i].value == val) {
            return iosCitys[i].id;
          }
        }
      }
    });
    //区
    Vue.filter("TextDistrict", function(val) {
      if (val && val != null && val != "undefined") {
        for (var i = 0; i < iosCountys.length; i++) {
          if (iosCountys[i].value == val) {
            return iosCountys[i].id;
          }
        }
      }
    });



  // 时间轴
  Vue.filter("timeshort", function(value) {
    var time =value ? value.replace("T", " ") : "";
    return time.substr(0,(time.length-3));
  });

  Vue.filter('getDate', function(value){
    var date = value ? value.split("T"):"";
    return date[0];
  });
  
  Vue.filter('fixed0', function(value){
    value = 1*value;
    var m = value ? value.toFixed(0) : 0;
    return m;
  });

  Vue.filter('fixed00', function(value){
    value = 1*value;
    var m = value ? value.toFixed(2) : 0;
    return m ;
  });

  Vue.filter("ydmhm", function(value) {
    if (value) {
      value = value.replace("T", " ");
      value = value.substr(0, value.length - 3);
    } else {
      value = "";
    }
    return value ? value : "";
  });

  Vue.filter("filterImgUrl", function(val) {
    if(val != null && val != "null" && val && val.indexOf("[")<0)
    {
      return "https://image.csyaoly.com" + val;
    }
    if (val != null && val != "null" && val && val != "[]") {
      val = JSON.parse(val);
      if (val.length == 1) {
        return "https://image.csyaoly.com" + val;
      } else {
        return "https://image.csyaoly.com" + val[0];
      }
    }
  });
  Vue.filter("simpleImgUrl", function(val) {
    console.log(val);
    if (val != null && val != "null" && val) {
      if (val.indexOf(".") != -1) {
        return "http://ylpt.csyaoly.com/UploadFiles" + val;
      }else
      {
        return "https://image.csyaoly.com" + val;
      }
    
    }
  });
  Vue.filter("simpleImgUrl2", function(val) {
    console.log(val);
    if (val != null && val != "null" && val) {
      if (val.indexOf(".") != -1) {
        return "http://ylpt.csyaoly.com/UploadFiles" + val;
      }else
      {
        return "https://image.csyaoly.com" + JSON.parse(val);
      }
    
    }
  });
  //订单状态转换
  Vue.filter("orderState", function(value) {
    switch (value) {
      case "11":
        return "待发单";
        break;
      case "12":
        return "待派单";
        break;
      case "44":
        return "已派单";
        break;
      case "51":
        return "待提货";
        break;
      case "52":
        return "配送中";
        break;
      case "56":
        return "已送达";
        break;
      case "81":
        return "已签收";
        break;
      case "10":
        return "已取消";
        break;
      case "82":
        return "异常";
        break;
      default:
        break;
    }
  });

  Vue.filter("orderStateC", function(value) {
    switch (value) {
      case "11":
        return "待发单";
        break;
      case "12":
        return "已派单";
        break;
      case "44":
        return "已派单";
        break;
      case "51":
        return "待提货";
        break;
      case "52":
        return "配送中";
        break;
      case "56":
        return "已送达";
        break;
      case "81":
        return "已签收";
        break;
      case "10":
        return "已取消";
        break;
      case "82":
        return "异常";
        break;
      default:
        break;
    }
  });

  Vue.filter("orderStateDrv", function(value) {
    switch (value) {
      case "11":
        return "待发单";
        break;
      case "12":
        return "待接单";
        break;
      case "51":
        return "待提货";
        break;
      case "52":
        return "待交货";
        break;
      case "56":
        return "待签收";
        break;
      case "81":
        return "已签收";
        break;
      case "10":
        return "已取消";
        break;
      default:
        break;
    }
  });

  Vue.filter("claimState", function(value){
    switch (value) {
      case "11":
        return "待审核";
        break;
      case "12":
        return "待提交资料";
        break;
      case "43":
        return "待定损";
        break;
      case "44":
        return "待收款";
        break;
      case "45":
        return "待支付";
        break;
      case "46":
        return "已赔付";
        break;
      case "61":
        return "理赔完成";
        break;
      default:
        break;
    }
  });

  Vue.filter("weight", function(value) {
      return value + "Kg";
  });
  Vue.filter("volumn", function(value) {
      return value + "m³";
  });

  Vue.filter("DateTransformation", function(value) {

    value =value ? value.replace("T", " ") : "";

    // var date = new Date();
    // var date1 =new Date(value);
    // var date3=date1.getTime()-date.getTime()  //时间差的毫秒数

    // var days=Math.floor(date3/(24*3600*1000)); //相差天数
    
    // var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
    // var hours = Math.floor(leave1 / (3600 * 1000));
    // var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
    // var minutes = Math.floor(leave2 / (60 * 1000));

    // console.log(date);
    // console.log(date1);
    // console.log(" 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟")

    // if(hours != 0)
    // {
    //   console.log(hours);
    //   return '已超时';
    // }
    // if (minutes < 0) {
    //   return '已超时'+ (0-minutes) +'分钟';
    // }
   
    // return minutes +'分钟';

    var u = navigator.userAgent;
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
    if(isiOS)
    {
      value = value.replace(/-/g,'/');
    }
    var date1 = new Date(value);
    var date2 = new Date();
    var total = 0;
    var s1 = date1.getTime();
    var s2 = date2.getTime();

    if(s1 < s2)
    {
      total = (s2 - s1)/1000;
    }
    if(s1 > s2)
    {
      total = 0-((s2 - s1)/1000);
    }

    var day = Math.floor(total/(24*60*60));//计算整数天数
    var afterDay = total-day*24*60*60;//取得算出天数后剩余的秒数
    var hour = Math.floor(afterDay/(60*60));//计算整数小时数
    var afterHour = total-day*24*60*60 - hour*60*60;//取得算出小时数后剩余的秒数
    var min = Math.floor(afterHour/60);//计算整数分
    var afterMin = total - day*24*60*60 - hour*60*60 - min*60;//取得算出分后剩余的秒数

    console.log("相差 "+day+"天 "+hour+"小时 "+min+" 分钟")
    if (day > 0) {
      return day+"天 "+hour+"小时 "+min+" 分钟";
    }

    if(hour == 0 && min != 0) //无小时，有分钟
    {
      if(s1 > s2)//还未到时间
      {
        return min + '分钟';
      }
      else if(s1 < s2) //已超时间
      {
        return '已超时' + min + '分钟';
      }
      else
      {
        return '即将超时'
      }
    }
    else if(hour == 0 && min == 0)
    {
      return '即将超时'
    }
    
    if(hour != 0 && min != 0) //有小时，有分钟
    {
      if(s1 >= s2)//还未到时间
      {
        return hour*60 + min + '分钟';
      }
      else if(s1 <= s2) //已超时间
      {
        return '已超时';
      }
      else
      {
        return '即将超时';
      } 
    }
    else if(hour == 0 && min == 0)
    {
      return '即将超时'
    }
  });

  Vue.filter("DriverFee",function(value){
    return ((value * 94)/100).toFixed(0);
  });
  Vue.filter("noslash",function(value){
    if(value)
    {
      return value.replace(/[\/]/g, '');
    }
    return value;
  });
}
